#  date 2013/2/17
#  name create Joints
#  author lizhi
#  E_mail  guishiye@sina.cn
#  QQ  294596787

import maya.cmds as mc

import sys
sys.path.append('E:/script/facial_sys')

import locatorClassify as lc
import commonScripts02
import createBridgeGrp02

reload(lc)

class buildBaseSetting(commonScripts02.commonScripts,createBridgeGrp02.createBridgeGrp):
    #-------------------------    create Joints ---------------------------
    def createJoints(self):
        mc.select('*_locator',r=True)
        objectsel=mc.ls(sl=True)
        
        self.getTypeObject(tpye='kLocator',objectsel=objectsel)
        objectsel=mc.ls(sl=True)
        #mc.group(em=True,n='jointRadius')
        #mc.addAttr('jointRadius',longName='Radius',defaultValue=1,k=1)
        jntsAll=[]
        for i in objectsel:
            
            mc.select(cl=True)
            jntname=str(i).replace('locator','jnt')
            if mc.objExists(jntname)== False:
            
                mc.joint(n=jntname)
                mc.parentConstraint(i,jntname,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                jntRX=mc.getAttr(str(jntname)+'.rx')
                jntRY=mc.getAttr(str(jntname)+'.ry')
                jntRZ=mc.getAttr(str(jntname)+'.rz')
                
                mc.setAttr(str(jntname)+'.jointOrient',jntRX,jntRY,jntRZ)
                mc.setAttr(str(jntname)+'.rotate',0,0,0)
                
                #mc.connectAttr('jointRadius.Radius',str(jntname)+'.radius')
            else:
                pass
            jntsAll.append(jntname)
        needSecondryjnt=lc.needSecondryjnt()
        
        for k in needSecondryjnt:
            mc.select(cl=True)
            secondryjntname=str(k).replace('locator','sec_jnt')
            if mc.objExists(secondryjntname)== False:
                mc.joint(n=secondryjntname)
                mc.setAttr(str(secondryjntname)+'.radius',0.5)
                parentjnt=str(k).replace('locator','jnt')
                mc.parentConstraint(k,secondryjntname,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                
                secjntRX=mc.getAttr(str(secondryjntname)+'.rx')
                secjntRY=mc.getAttr(str(secondryjntname)+'.ry')
                secjntRZ=mc.getAttr(str(secondryjntname)+'.rz')
                
                mc.setAttr(str(secondryjntname)+'.jointOrient',secjntRX,secjntRY,secjntRZ)
                mc.setAttr(str(secondryjntname)+'.rotate',0,0,0)
                
                mc.parent(secondryjntname,parentjnt)
               
            else:
                pass
            jntsAll.append(secondryjntname)
        return jntsAll
    
    #-------------------------    parent Relations ---------------------------
    def parentRelations(self):
    
        #   mouth joint Relations 
        facial_ctrl_grp='facial_ctrl_grp'
        if mc.objExists(facial_ctrl_grp)== False:
        
            mouthChildJoint=lc.mouthChildObj()
            
            mc.select('Mouth_C_All_jnt',r=True)
            mouthParentJoint=mc.ls(sl=True)
            
            mc.parent(mouthChildJoint,mouthParentJoint)
            #   eye joint Relations
            #   eye L
            eyeLfChildObj=lc.eyeLfChildObj()
            mc.parent(eyeLfChildObj,'facial_L_eyeCenter_jnt')
            #   eye R
            eyeRtChildObj=lc.eyeRtChildObj()
            mc.parent(eyeRtChildObj,'facial_R_eyeCenter_jnt')
            
            #   eyeBrow L
            eyeBrownLfObj=lc.eyeBrownLfObj()
            mc.parent(eyeBrownLfObj,'facial_L_EyeBrow_jnt')
            #   eyeBrow R
            eyeBrownRtObj=lc.eyeBrownRtObj()
            mc.parent(eyeBrownRtObj,'facial_R_EyeBrow_jnt')
            #   nose C
            noseChildObj=lc.noseChildObj()
            mc.parent(noseChildObj,'facial_C_Nose_jnt')
            #     tongue C
            tongueObj=lc.tongueObj()
            for i in range(len(tongueObj)-1):
                mc.parent(tongueObj[i],tongueObj[i+1])
                
            #    head base
            headBaseObj=lc.headBaseObj()
            for h in range(len(headBaseObj)-1):
                mc.parent(headBaseObj[h],headBaseObj[h+1])
            mc.parent('headTop_jnt',headBaseObj[-2])
            
            mc.parent('facial_C_tongue01_jnt','Jaw02_jnt')
            mc.parent('facial_C_lowerTooth_jnt','Jaw02_jnt')
            mc.parent('facial_C_upperTooth_jnt',headBaseObj[-2])
            ###group the joints
            seconParentObj=lc.seconParentObj()
            mc.group(em=True,n=facial_ctrl_grp)
            mc.parent(seconParentObj,facial_ctrl_grp)

        else:
            pass
            
        return(facial_ctrl_grp)
    
    #------------------------   add setDriven key group  -----------------------------
    def addSDKGrp(self):
        grpAll=['zero','con','sdk','ctrl']
        facialObjects=lc.facialObj()
    
        for i in facialObjects:
            for k in grpAll:
                mc.select(i)
                self.makeZeroGrp(sufname=k,Mode=1)
    #-----------------------   add offset and root grp --------------------------------
    def offsetRootGrp(self):
        offsetRootClassify=lc.offsetRootClassify()
        grpSufAll=['offset_zero','offset','root']
        for i in offsetRootClassify:
            for k in grpSufAll:
                mc.select(i)
                self.makeZeroGrpTwo(sufname=k,cenObj='Jaw01_jnt',Mode=1)
                
    #######======================================================================================
    # ------------------------------ add other things beload the group of facial_jnt -----------------------
    def facial_jntOther(self):
        #------------- add group empoty  -------------------------
        locmouthuplipgrpnamelow='Mouth_follow_grp'
        if mc.objExists(locmouthuplipgrpnamelow)== False:
            facial_Puffer=lc.facial_Puffer()
          
            for i in facial_Puffer:
                emgroup=str(i).replace('_locator','FollowHead_grp')
                mc.group(em=True,n=emgroup)
                mc.parentConstraint(i,emgroup,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                mc.parent(emgroup,'facial_jnt')
               ######   
                emjawgroup=str(i).replace('_locator','FollowJaw_grp')
                mc.group(em=True,n=emjawgroup)
                mc.parentConstraint(i,emjawgroup,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                mc.parent(emjawgroup,'Jaw02_jnt')
                
            ######
            
            jaw01Locators=lc.jaw01Locator()
          
            sufname=['Rot_drv','UpperRot_drv']
            
            for k in jaw01Locators:
                for m in sufname:
                    jaw01jnt=str(k).replace('_locator','_jnt')
                    emRotDrvgroup=str(k).replace('_locator',m)
                    mc.group(em=True,n=emRotDrvgroup)
                    mc.parentConstraint(k,emRotDrvgroup,mo=False,n='templeconstraint')
                    mc.delete('templeconstraint')
                    mc.parent(emRotDrvgroup,jaw01jnt)
                    
            ####  add ctrl group and sdk to Jaw02_jnt
            jaw02Locator=lc.jaw02Locator()[0]
            jaw01Locator=lc.jaw01Locator()[0]
            
            jntjaw02name=str(jaw02Locator).replace('_locator','_jnt')
            jntjaw01name=str(jaw01Locator).replace('_locator','_jnt')
            jntsdkjaw02name=str(jaw02Locator).replace('_locator','_sdk')
            
            mc.select(jntjaw02name)
            
            self.LZ_creatJoint(jaw01Locator,jntsdkjaw02name)
            mc.parent(jntsdkjaw02name,'Jaw01_jnt')
            

            for grpreg in [jntsdkjaw02name,jntjaw01name]:
          
                mc.select(grpreg)
                
                self.makeZeroGrpTwo(sufname='zero',cenObj=jaw01Locator,Mode=1)
            mc.select(jntjaw02name)
            grpname=self.makeZeroGrpTwo(sufname='ctrl',cenObj=jaw01Locator,Mode=1)
            mc.parent(grpname,jntsdkjaw02name)
            
            mc.select(cl=True)
            #####    create Mouth_follow group and locators 
            
            mouthChildObj=lc.mouthChildObj()
            locmouthuplipgrpname='Mouth_followHead_grp'
            locmouthuplipgrpnamelow='Mouth_follow_grp'
            mc.group(em=True,n=locmouthuplipgrpname)
            mc.group(em=True,n=locmouthuplipgrpnamelow)
            mc.parent(locmouthuplipgrpname,'facial_jnt')
            
            for loc in mouthChildObj:
                mc.select(cl=True)
                locatorname=str(loc).replace('_jnt','_follow_upLip')
                mc.spaceLocator(n=locatorname)
                mc.parentConstraint('Jaw01_locator',locatorname,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                mc.parent(locatorname,locmouthuplipgrpname)
                mc.select(locatorname)
                self.makeZeroGrpTwo(sufname='zero',cenObj='',Mode=0)
                mc.select(cl=True)
                
                mc.select(cl=True)
                locatornamelow=str(loc).replace('_jnt','_follow_lowLip')
                mc.spaceLocator(n=locatornamelow)
                mc.parentConstraint('Jaw01_locator',locatornamelow,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                mc.parent(locatornamelow,locmouthuplipgrpnamelow)
                mc.select(locatornamelow)
                self.makeZeroGrpTwo(sufname='zero',cenObj='',Mode=0)
                mc.select(cl=True)
                
            mc.setAttr(str(locmouthuplipgrpname)+'.visibility',0)
            mc.setAttr(str(locmouthuplipgrpnamelow)+'.visibility',0)
            #####   create eye jnt
            mc.select(cl=True)
            eyeLocator=lc.eyeLocator()
            for eyeloc in eyeLocator:
                eyejntname=str(eyeloc).replace('Center_locator','_jnt')
                self.LZ_creatJoint(eyeloc,eyejntname)
          
                mc.parent(eyejntname,'facial_jnt')
                mc.setAttr(str(eyejntname)+'.radius',0.5)
                self.makeZeroGrp(sufname='_zero',Mode=0)
            ###   group neckRootlocator 
            neckRootlocator=lc.neckRootlocator()[0]
            neckRootjot=str(neckRootlocator).replace('_locator','_jnt')
            mc.select(neckRootjot)
            neckRootGrp = self.makeZeroGrpTwo(sufname='_zero',cenObj=lc.faciallocator()[0],Mode=0)
            
            ########   parentConstraint  the facial_Puffer  with jaw and facial_L_PufferFollowJaw_grp
            
            mc.parentConstraint('facial_L_PufferFollowJaw_grp','facial_L_PufferFollowHead_grp','facial_L_Puffer_zero',mo=True)
            mc.parentConstraint('facial_R_PufferFollowJaw_grp','facial_R_PufferFollowHead_grp','facial_R_Puffer_zero',mo=True)
            
        else:
            pass
        return(neckRootGrp,locmouthuplipgrpnamelow)
    def eyeModifyRelations(self):
        ##################  up date  on 2013/4/1
        ######   modify the relationns of eye
        mc.group(em=True,n='L_upperEyelid_drv')
        mc.parent('L_upperEyelid_drv','facial_L_eyeCenter_jnt')
        mc.setAttr('L_upperEyelid_drv.translate',0,0,0)
        mc.setAttr('L_upperEyelid_drv.rotate',0,0,0)
        
        mc.aimConstraint('facial_L_UpperEyelid_locator','L_upperEyelid_drv',aimVector=[0,0,1],upVector=[0,1,0],n='aimtempleCon',mo=False)
        self.makeZeroGrpTwo(sufname='_zero',cenObj='L_upperEyelid_drv',Mode=0)
        self.makeZeroGrpTwo(sufname='_offset',cenObj='L_upperEyelid_drv',Mode=0)
        mc.delete('aimtempleCon')
        mc.parent('facial_L_UpperEyelid_zero','facial_L_UpperInnerEyelid_zero','facial_L_UpperOuterEyelid_zero','L_upperEyelid_drv')
        
        mc.group(em=True,n='R_upperEyelid_drv')
        mc.parent('R_upperEyelid_drv','facial_R_eyeCenter_jnt')
        mc.setAttr('R_upperEyelid_drv.translate',0,0,0)
        mc.setAttr('R_upperEyelid_drv.rotate',0,0,0)
        
        mc.aimConstraint('facial_R_UpperEyelid_locator','R_upperEyelid_drv',aimVector=[0,0,1],upVector=[0,1,0],n='aimtempleCon',mo=False)
        self.makeZeroGrpTwo(sufname='_zero',cenObj='R_upperEyelid_drv',Mode=0)
        self.makeZeroGrpTwo(sufname='_offset',cenObj='R_upperEyelid_drv',Mode=0)
        mc.delete('aimtempleCon')
        mc.parent('facial_R_UpperEyelid_zero','facial_R_UpperInnerEyelid_zero','facial_R_UpperOuterEyelid_zero','R_upperEyelid_drv')
        
        ######## update on 2013/7/4
        #####  modify the relations of low eye
        mc.group(em=True,n='L_lowerEyelid_drv')
        mc.parent('L_lowerEyelid_drv','facial_L_eyeCenter_jnt')
        mc.setAttr('L_lowerEyelid_drv.translate',0,0,0)
        mc.setAttr('L_lowerEyelid_drv.rotate',0,0,0)
        
        mc.aimConstraint('facial_L_LowerEyelid_locator','L_lowerEyelid_drv',aimVector=[0,0,1],upVector=[0,1,0],n='aimtempleCon',mo=False)
        self.makeZeroGrpTwo(sufname='_zero',cenObj='L_lowerEyelid_drv',Mode=0)
        self.makeZeroGrpTwo(sufname='_offset',cenObj='L_lowerEyelid_drv',Mode=0)
        mc.delete('aimtempleCon')
        mc.parent('facial_L_LowerOuterEyelid_zero','facial_L_LowerEyelid_zero','facial_L_LowerInnerEyelid_zero','L_lowerEyelid_drv')
        
        mc.group(em=True,n='R_lowerEyelid_drv')
        mc.parent('R_lowerEyelid_drv','facial_R_eyeCenter_jnt')
        mc.setAttr('R_lowerEyelid_drv.translate',0,0,0)
        mc.setAttr('R_lowerEyelid_drv.rotate',0,0,0)
        
        mc.aimConstraint('facial_R_LowerEyelid_locator','R_lowerEyelid_drv',aimVector=[0,0,1],upVector=[0,1,0],n='aimtempleCon',mo=False)
        self.makeZeroGrpTwo(sufname='_zero',cenObj='R_lowerEyelid_drv',Mode=0)
        self.makeZeroGrpTwo(sufname='_offset',cenObj='R_lowerEyelid_drv',Mode=0)
        mc.delete('aimtempleCon')
        mc.parent('facial_R_LowerOuterEyelid_zero','facial_R_LowerEyelid_zero','facial_R_LowerInnerEyelid_zero','R_lowerEyelid_drv')
        
        
            ##-------------------------  create eye ctrls -----------------------------------
    def createEyeCtrl(self):    
        eye_name=['LeyeCircle','Eyes_ctrl']
        EyeAim_jot=['L_EyeAim_jot','R_EyeAim_jot']
        singleEyectrlname=['L_EyeAim_ctrl','R_EyeAim_ctrl']
        cPosX=[-2.0,2.0]
        eyejntAll=['facial_L_eye_jnt','facial_R_eye_jnt']
        eyedisPos=[]
        Eyes_grp='Eyes_grp'
        eyeCtrlTZ=20
        if mc.objExists(eyejntAll[0])== True:
            for jntreg in eyejntAll:
                objpos=self.getWorldPosition(jntreg)
           
                eyedisPos.append(objpos)
            if len(eyedisPos)==2:
           
                eyesDistance=self.disBetweenPoints(eyedisPos[0],eyedisPos[1])
           
            else:
                sys.stdout.write('" point arguments is not two, please check it  "')
           
            cPosX[0]=cPosX[0]*(eyesDistance/4.0)
            cPosX[1]=cPosX[1]*(eyesDistance/4.0)
           
            radius=1.5*(eyesDistance/4.0)
            
            if mc.objExists(eye_name[1])== False:
                for i in range(len(eye_name)):
                    mc.circle(c=(cPosX[i],0,0),nr=(0,0,1),sw=360,r=radius,d=3,ut=0,tol=0.01,s=8,ch=0,n=eye_name[i])
                    mc.select(cl=True)
                    mc.joint(n=EyeAim_jot[i])
                    mc.move(cPosX[i],0,0,EyeAim_jot[i])
                    self.createsingleEyectrl(singleEyectrlname[i])
                    mc.parentConstraint(EyeAim_jot[i],singleEyectrlname[i],mo=False,n='templeconstraint')
                    mc.delete('templeconstraint')
                    mc.parent(EyeAim_jot[i],singleEyectrlname[i])
                    
                
                LeyeShap=mc.listRelatives(eye_name[0],s=True)[0]
                mc.parent(LeyeShap,eye_name[1],shape=True,add=True)
                
                mc.delete(eye_name[0])
                mc.parent(singleEyectrlname[0],singleEyectrlname[1],eye_name[1])
                for k in singleEyectrlname:
                    mc.select(k)
                    self.makeZeroGrp(sufname='zero',Mode=0)
                
                mc.setAttr(str(eye_name[1])+'.ty',eyedisPos[0][1])
                
                facialBasePosition=self.getWorldPosition('facial_locator')
                eyeCtrlTZ=eyeCtrlTZ*((abs(facialBasePosition[2]-objpos[2]))/5.5)
                print(eyeCtrlTZ)
                mc.setAttr(str(eye_name[1])+'.tz',eyeCtrlTZ)
                ##  group zero ctrls

                EyeAim_jot.append(eye_name[1])

                for ctrlzero in EyeAim_jot:
                    mc.select(ctrlzero)
                    self.makeZeroGrp(sufname='_zero',Mode=0)

                mc.select(cl=True)
                mc.group(em=True,n=Eyes_grp)
                mc.parent(str(eye_name[1])+'_zero',Eyes_grp)
                
                locfacial=lc.faciallocator()[0]
                jotFacial=str(locfacial).replace('_locator','_jnt')
                
                ##   create eye follow head locator
                locFollowHead=str(eye_name[1]).replace('_ctrl','CtrlFol_head')
                mc.spaceLocator(n=locFollowHead)
                mc.parentConstraint(eye_name[1],locFollowHead,mo=False,n='templeconstraint')
                mc.delete('templeconstraint')
                mc.parent(locFollowHead,Eyes_grp,jotFacial)
            else:
                sys.stdout.write('" Eyes_ctrl has existed ",  please ask yourself " if I am a foolish ??? fuck  !! "')
        else:
            sys.stdout.write(str(eyejntAll[0])+' has existed ,  please ask yourself " if I am a foolish ??? fuck  !! "')
        mc.select(cl=True)
        
        
        
    #==============================================================================================
    #---------------  Organize a hierarchy relations  ------------------------------------
    def OrganizeHierarchyRelations(self):
        bridgeAllname='facial_bridge_grp'
        if mc.objExists(bridgeAllname)== False:
            createMouth=self.createMouthBridge()
            createEyes=self.createEyesBridge()
            createNose=self.createNoseBridge()
            createFacial=self.createFacialBridge()
            createPS_normal=self.createPS_normalBridge()
            createPS_Smile=self.createPS_SmileBridge()
            createPS_Frown=self.createPS_FrownBridge()

            mc.group(em=True,n=bridgeAllname)
            mc.parent(createMouth,createEyes,createNose,createFacial,createPS_normal,createPS_Smile,createPS_Frown,bridgeAllname)
        else:
            sys.stdout.write('have already exist facial_bridge_grp')
                
        return(bridgeAllname)
            
    def addCtrlToSettings(self):
        scalRelative=0.2
        
        positionStaf01=self.getWorldPosition('facial_L_eyeCenter_locator')
        positionStaf02=self.getWorldPosition('facial_R_eyeCenter_locator')

        disBetReg=self.disBetweenPoints(positionStaf01,positionStaf02)

        scalRelative=scalRelative*(disBetReg/3.5)
        
        facialObj=lc.facialObj()
        facialObjctrl=str(facialObj).replace('_jnt','ctrl')
        for i in range(len(facialObj)):
            self.ctrlmakeToFacial(facialObj[i])

            ctrlsname=str(facialObj[i]).replace('_jnt','_ctrl')
            self.setCtrlColor(17,ctrlsname)
            self.scaleCurveHull(ctrlsname,scalRelative,scalRelative,scalRelative)
            
        ###   set grpCtrl color and volumn
        grplocator=lc.grplocator()
        
        for k in grplocator:
            ctrlgrpname=str(k).replace('_locator','_ctrl')
            self.setCtrlColor(13,ctrlgrpname)
            self.scaleCurveHull(ctrlgrpname,2,2,2)

    def followMouthSettings(self):
        jawCtrl='Jaw02_ctrl'
        mouthFollowLocator=['Mouth_L_Corner_follow_lowLip','Mouth_L_LowLipCenter_follow_lowLip','Mouth_L_UpLipCenter_follow_lowLip','Mouth_R_Corner_follow_lowLip','Mouth_R_LowLipCenter_follow_lowLip','Mouth_R_UpLipCenter_follow_lowLip','Mouth_C_LowLip_follow_lowLip','Mouth_C_UpLip_follow_lowLip']
        
        mouthbridgeAttr=['facial_mouth_bridge.L_lip_corner_follow','facial_mouth_bridge.L_low_lip_center_follow','facial_mouth_bridge.L_up_lip_center_follow','facial_mouth_bridge.R_lip_corner_follow','facial_mouth_bridge.R_low_lip_center_follow','facial_mouth_bridge.R_up_lip_center_follow','facial_mouth_bridge.C_low_lip_follow','facial_mouth_bridge.C_up_lip_follow']
        
        '''mouthConEdCtrls=[Mouth_L_LowLipCenter_root Mouth_C_LowLip_root Mouth_C_UpLip_root Mouth_L_Corner_root Mouth_L_UpLipCenter_root Mouth_R_Corner_root Mouth_R_LowLipCenter_root Mouth_R_UpLipCenter_root Mouth_C_All_root]'''
        
        numstart=0
        for i in mouthFollowLocator:
            
            mc.parentConstraint(jawCtrl,i,mo=True)
            mouthConEdCtrl=str(i).replace('_follow_lowLip','_root')
            
            followUpLipLocator=str(i).replace('_lowLip','_upLip')
            mc.createNode('blendColors',n=str(i)+'_t_blend')
            mc.createNode('blendColors',n=str(i)+'_r_blend')
            
            mc.connectAttr(str(followUpLipLocator)+'.translate',str(i)+'_t_blend.color2')
            mc.connectAttr(str(i)+'.translate',str(i)+'_t_blend.color1')
            mc.connectAttr(str(followUpLipLocator)+'.rotate',str(i)+'_r_blend.color2')
            mc.connectAttr(str(i)+'.rotate',str(i)+'_r_blend.color1')
            
            
            mc.connectAttr(mouthbridgeAttr[numstart],str(i)+'_t_blend.blender')
            mc.connectAttr(mouthbridgeAttr[numstart],str(i)+'_r_blend.blender')
            
            mc.connectAttr(str(i)+'_t_blend.output',str(mouthConEdCtrl)+'.translate')
            mc.connectAttr(str(i)+'_r_blend.output',str(mouthConEdCtrl)+'.rotate')
            
            numstart=numstart+1
            
        